﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class Brand : BasePage
    {
        private IBrandBLL brandBLL = IocContainer.Instance.Resolve<IBrandBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if(!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnAdd, btnUpdate, btnDelete, btnSearch }, 9);
                this.BrandTableBody.Text = GetBrandInfo("", null);
            }
        }

        private string GetBrandInfo(string strWhere, Hashtable parameter)
        {
            StringBuilder strBrandTable = new StringBuilder();
            DataTable dtRole = brandBLL.GetBrand(strWhere, parameter);
            if (dtRole != null && dtRole.Rows.Count > 0)
            {
                foreach (DataRow dataRow in dtRole.Rows)
                {
                    strBrandTable.AppendFormat("<tr rel='{0}'>", dataRow["BrandId"]);
                    strBrandTable.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td></tr>", dataRow["BrandCode"], dataRow["BrandName"], dataRow["Description"]);
                }
            }
            return strBrandTable.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string brandName = txtBrandName.Text.Trim();
                if (!string.IsNullOrEmpty(brandName))
                {
                    Hashtable parameter = new Hashtable();
                    parameter.Add("brandName", "%" + brandName + "%");
                    this.BrandTableBody.Text = GetBrandInfo(" and BrandName like @brandName order by BrandName asc ", parameter);
                }
                else
                {
                    this.BrandTableBody.Text = GetBrandInfo("", null);
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
            
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int BrandId = 0;
                int.TryParse(this.selectedBrandID.Value.Trim(), out BrandId);
                brandBLL.Delete(BrandId);

                this.BrandTableBody.Text = GetBrandInfo(string.Empty, null);
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
            
        }
    }
}
